﻿using System;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using umbraco.presentation.nodeFactory;
using System.Web;
using System.Web.Services;
using greenwerkz.constructs;
using greenwerkz.DAL;
using System.Data;

namespace greenwerkz.webservices
{
    /// <summary>
    /// Summary description for Plants
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]

    public class Plants : System.Web.Services.WebService
    {
        public string plantPhotoDir
        {
            get { return ConfigurationManager.AppSettings.Get("gw_PlantDir").ToString(); }
        }
        public class KeyValuePair
        {

            public int Key;
            public string Value;
            public KeyValuePair()
            {
            }
            public KeyValuePair(int NewValue, string NewDescription)
            {
                Key = NewValue;
                Value = NewDescription;
            }

            public override string ToString()
            {
                return Value;
            }
        }
        public Plants()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int getNextPlantID()
        {
            return int.Parse(greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_selectNextPlantID()[0][0].ToString());
        }
        [WebMethod]
        public List<KeyValuePair> getPlantState()
        {
            List<KeyValuePair> returnDict = new List<KeyValuePair>();
            foreach (string item in Enum.GetNames(typeof(greenwerkz.DAL.gw_PlantState)))
            {
                int value = (int)Enum.Parse(typeof(greenwerkz.DAL.gw_PlantState), item);
                returnDict.Add(new KeyValuePair(value, item));
            }
            return returnDict;
        }
        [WebMethod]
        public List<KeyValuePair> getPlantCondition()
        {
            List<KeyValuePair> returnDict = new List<KeyValuePair>();
            foreach (string item in Enum.GetNames(typeof(greenwerkz.DAL.gw_plantCondition)))
            {
                int value = (int)Enum.Parse(typeof(greenwerkz.DAL.gw_plantCondition), item);
                returnDict.Add(new KeyValuePair(value, item));
            }
            return returnDict;
        }
        [WebMethod]
        public List<KeyValuePair> getPlantLocation()
        {
            List<KeyValuePair> returnDict = new List<KeyValuePair>();
            foreach (string item in Enum.GetNames(typeof(greenwerkz.DAL.gw_plantLocation)))
            {
                int value = (int)Enum.Parse(typeof(greenwerkz.DAL.gw_plantLocation), item);
                returnDict.Add(new KeyValuePair(value, item));
            }
            return returnDict;
        }
        [WebMethod]
        public List<strain> getStrains()
        {
            List<strain> returnStrain = new List<strain>();
            //hard code strains node for now
            Node node = new umbraco.presentation.nodeFactory.Node(1128);
            umbraco.presentation.nodeFactory.Nodes childrenNodes = node.Children;
            foreach (Node oChildNode in node.Children)
            {
                returnStrain.Add(new strain(oChildNode.Id));
            }
            return returnStrain;
        }
        [WebMethod]
        public string InsertPlantPicture(int l_iPlantStateID, int l_iPlantID, Guid l_gPlantGUID, Byte[] l_sPlantPicture, string l_sPlantExtension)
        {
            try
            {
                // Convert back to image binary stream

                //byte[] byteresponse = Convert.FromBase64String(l_sPlantPicture);

                //MemoryStream stream2 = new MemoryStream(byteresponse);
                string sFilePath = plantPhotoDir + l_gPlantGUID.ToString() + l_sPlantExtension;
                FileStream fs =
                    new FileStream(Server.MapPath(sFilePath),
                        FileMode.Create,
                        FileAccess.ReadWrite);
                fs.Write(l_sPlantPicture, 0, l_sPlantPicture.Length);
                fs.Flush();
                //fs.Write(stream2.GetBuffer(), 0, (int) stream2.Position);
                fs.Close();
                //build the url 
                string sPictureURL = ConfigurationManager.AppSettings.Get("gw_baseURL").ToString() + sFilePath;
                //save to db

                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_insertPlantPicture
                    (l_iPlantID
                    , l_gPlantGUID
                    , l_iPlantStateID
                    , l_sPlantExtension
                    , sPictureURL
                    );
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        [WebMethod]
        public bool SelectPlantByPlantID(int l_PlantID)
        {
            return true;
        }
        [WebMethod]
        public bool setFlowerDate(int l_plantID)
        {
            //linq patient cache to get total # of potential candidates
            try
            {
                DB_1505_umbracoGenerated.gwGrow_setFlowerDateByPlantID(l_plantID);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public int selectRandomPatient(int l_stateID)
        {
            //linq patient cache to get total # of potential candidates
            return 10;
        }
        [WebMethod]
        public bool InsertPlant(int l_growLocationID, string l_plantTagID, int l_PlantID, int l_PlantStateID, int l_PlantStrainNodeID, int l_MemberID, int l_PlantLocationID)
        {
            try
            {
                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_insertPlant
                    (l_growLocationID
                    ,l_plantTagID
                    ,l_PlantID
                    , l_PlantStateID
                    , l_PlantStrainNodeID
                    , l_MemberID
                    , l_PlantLocationID
                    );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public bool ChangePlantState(int l_PlantID, int l_PlantStateID)
        {
            try
            {
                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_updatePlantState(
                    l_PlantID
                    , l_PlantStateID
                    );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public bool ChangePlantLocation(int l_PlantID, int l_PlantLocationID)
        {
            try
            {
                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_updatePlantLocation(
                    l_PlantID
                    , l_PlantLocationID
                    );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public List<Plant> selectReservedPlantsByGrowOperation(int l_locationID)
        {
            return Plant.ReservedPlantsByGrowOperationID(l_locationID);
        }
        [WebMethod]
        public bool insertPlantComment(int l_PlantID, string l_sPlantComment, int l_iUserID)
        {
            try
            {
                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_insertPlantComment(
                    l_PlantID
                    , l_sPlantComment
                    , l_iUserID
                    );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public bool insertPlantWeight(int l_PlantID, int l_userID, short l_weightType, double l_weight)
        {
            try
            {
                greenwerkz.DAL.DB_1505_umbracoGenerated.gwGrow_insertPlantWeight(
                    l_PlantID
                    , l_userID
                    , l_weightType
                    , l_weight
                    );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        [WebMethod]
        public Plant getPlantInfo(int l_PlantID)
        {
            try
            {
                return new Plant(l_PlantID);
            }
            catch (Exception ex)
            {
                return new Plant();
            }
        }
        [WebMethod]
        public Plant getPlantInfoByTag(string l_PlantTagID)
        {
            try
            {
                return new Plant(l_PlantTagID);
            }
            catch (Exception ex)
            {
                return new Plant();
            }
        }
    }

}